﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class TargetManagementControl : UserControl
    {
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblFilmTableAdapter x_tblFilmTableAdapter = new tblFilmTableAdapter();
        private List<ReportDataSet.tblFilmRow> x_ListFilm = null;
        //
        private CinemaFilmTargetViewTableAdapter x_CinemaFilmTargetViewTableAdapter = new CinemaFilmTargetViewTableAdapter();
        private List<ReportDataSet.CinemaFilmTargetViewRow> x_ListTarget = null;
        ReportDataSet.CinemaFilmTargetViewRow x_CurrentTarget = null;
        //
        private tblTargetTableAdapter x_tblTargetTableAdapter = new tblTargetTableAdapter();

        //
        public TargetManagementControl()
        {
            InitializeComponent();
            initDataGridView();
        }
        private void TargetManagementControl_Load(object sender, EventArgs e)
        {
            cbFilm.DataSource = ListFilm;
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
            cbFilm.DisplayMember = "FilmTitle";
            gridTarget.DataSource = ListTarget;
        }
        private void initDataGridView()
        {
            initTargetDataGridView();
        }
        private void initTargetDataGridView()
        {
            gridTarget.AutoGenerateColumns = false;
            gridTarget.ReadOnly = true;

            DataGridViewTextBoxColumn targetIDColumn = new DataGridViewTextBoxColumn();
            targetIDColumn.DataPropertyName = "TargetID";
            targetIDColumn.HeaderText = "TargetID";

            DataGridViewTextBoxColumn IDColumn = new DataGridViewTextBoxColumn();
            IDColumn.DataPropertyName = "FilmTitle";
            IDColumn.HeaderText = "Film Title";

            DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
            nameColumn.DataPropertyName = "CinemaName";
            nameColumn.HeaderText = "Cinema Name";

            DataGridViewTextBoxColumn screen1Column = new DataGridViewTextBoxColumn();
            screen1Column.DataPropertyName = "FromDate";
            screen1Column.HeaderText = "Start Date";

            DataGridViewTextBoxColumn screen2Column = new DataGridViewTextBoxColumn();
            screen2Column.DataPropertyName = "ToDate";
            screen2Column.HeaderText = "Finish Date";

            DataGridViewTextBoxColumn screen3Column = new DataGridViewTextBoxColumn();
            screen3Column.DataPropertyName = "AdmitTarget";
            screen3Column.HeaderText = "AdmitTarget";

            DataGridViewTextBoxColumn boColumn = new DataGridViewTextBoxColumn();
            boColumn.DataPropertyName = "BOTarget";
            boColumn.HeaderText = "BOTarget";

            DataGridViewTextBoxColumn coColumn = new DataGridViewTextBoxColumn();
            coColumn.DataPropertyName = "COTarget";
            coColumn.HeaderText = "COTarget";

            gridTarget.Columns.Add(targetIDColumn);
            gridTarget.Columns.Add(IDColumn);
            gridTarget.Columns.Add(nameColumn);
            gridTarget.Columns.Add(screen1Column);
            gridTarget.Columns.Add(screen2Column);
            gridTarget.Columns.Add(screen3Column);
            gridTarget.Columns.Add(boColumn);
            gridTarget.Columns.Add(coColumn);
        }

        private void bindingTargetData()
        {
            if (CurrentTarget != null)
            {
                txtTargetID.Text = CurrentTarget.TargetID.ToString();
                cbFilm.Text = CurrentTarget.FilmTitle;
                cbCinema.Text = CurrentTarget.CinemaName;
                dpickerStartTarget.Value = CurrentTarget.FromDate;
                dpickerFinishTarget.Value = CurrentTarget.ToDate;
                txtAdmitTarget.Text = CurrentTarget.AdmitTarget.ToString();
                txtBOTarget.Text = CurrentTarget.BOTarget.ToString();
                txtCOTarget.Text = CurrentTarget.COTarget.ToString();
            }
        }
        public ReportDataSet.CinemaFilmTargetViewRow CurrentTarget
        {
            get
            {
                if (x_CurrentTarget == null)
                {
                    if (gridTarget.CurrentRow != null)
                    {
                        x_CurrentTarget = ListTarget[gridTarget.CurrentRow.Index];
                    }
                }
                return x_CurrentTarget;
            }
            set
            {
                x_CurrentTarget = value;
            }
        }
        public List<ReportDataSet.tblFilmRow> ListFilm
        {
            get
            {
                if (x_ListFilm == null)
                {
                    x_ListFilm = x_tblFilmTableAdapter.GetData().ToList();
                }
                return x_ListFilm;
            }
            set
            {
                x_ListFilm = value;
            }
        }
        public List<ReportDataSet.CinemaFilmTargetViewRow> ListTarget
        {
            get
            {
                if (x_ListTarget == null)
                {
                    x_ListTarget = x_CinemaFilmTargetViewTableAdapter.GetFilmTarget().ToList();
                }
                return x_ListTarget;
            }
            set
            {
                x_ListTarget = value;
            }
        }
        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void gridTarget_SelectionChanged(object sender, EventArgs e)
        {
            x_CurrentTarget = null;
            bindingTargetData();
        }

        private void btnSaveTarget_Click(object sender, EventArgs e)
        {
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            int filmID = ((ReportDataSet.tblFilmRow)cbFilm.SelectedItem).ID;
            int AdmitTarget = int.Parse(txtAdmitTarget.Text);
            int BOTarget = int.Parse(txtBOTarget.Text);
            int COTarget = int.Parse(txtCOTarget.Text);
            DateTime FromDate = dpickerStartTarget.Value;
            DateTime toDate = dpickerFinishTarget.Value;
            //
            ReportDataSet.tblTargetDataTable t = new ReportDataSet.tblTargetDataTable();
            x_tblTargetTableAdapter.Insert(CinemaID, filmID, AdmitTarget, BOTarget, COTarget, FromDate, toDate);

            ListTarget = null;
            gridTarget.DataSource = ListTarget;
        }

        private void btnUpdateTarget_Click(object sender, EventArgs e)
        {
            int targetID = int.Parse(txtTargetID.Text);
            //
            ReportDataSet.tblTargetRow a1 = x_tblTargetTableAdapter.GetData()
                .Where(kvp => kvp.ID == targetID)
                .First();
            //
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            int filmID = ((ReportDataSet.tblFilmRow)cbFilm.SelectedItem).ID;
            int AdmitTarget = int.Parse(txtAdmitTarget.Text);
            int BOTarget = int.Parse(txtBOTarget.Text);
            int COTarget = int.Parse(txtCOTarget.Text);
            DateTime FromDate = dpickerStartTarget.Value;
            DateTime toDate = dpickerFinishTarget.Value;
            //
            a1.CinemaID = CinemaID;
            a1.FilmID = filmID;
            a1.AdmitTarget = AdmitTarget;
            a1.BOTarget = BOTarget;
            a1.COTarget = COTarget;
            //
            a1.FromDate = dpickerStartTarget.Value;
            a1.ToDate = dpickerFinishTarget.Value;

            x_tblTargetTableAdapter.Update(a1);

            ListTarget = null;
            gridTarget.DataSource = ListTarget;
        }
    }
}
